﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfeccionesJR.Entites;
using System.Data;
using System.Data.Objects;


namespace ConfeccionesJR.Repository
{
    public class VentaRepository: MasterDataAccess, IVentaRepository
    {

        public List<VENTA> GetAllFromVentas()
        {
            var hoy = DateTime.Today.Date;
            var query = from c in Context.VENTAs.Include("CLIENTE")
                        orderby c.Fecha descending
                        //where c.Fecha == hoy
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            
            return query.ToList();
        }

        public List<VENTA> GetFromVentabyCliente(string cliente)
        {
            if (cliente != "")
            {
                var query = from v in Context.VENTAs.Include("CLIENTE")
                            where v.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper()) || 
                                    v.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper())
                            select v;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);
                
                return query.ToList();
            }
            else
            {
                var query = from v in Context.VENTAs.Include("CLIENTE")
                            select v;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);
                
                return query.Take(1000).ToList();
            }
        }

        public List<VENTA> GetFromVentabyClienteAndFecha(string cliente, DateTime? fecha)
        {
            if (cliente != null && cliente != string.Empty && cliente != "" && fecha != null)
            {
                var query = from v in Context.VENTAs.Include("CLIENTE")
                            where 
                            (
                            v.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper())|| 
                            v.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper())
                            )
                            && (v.Fecha == fecha.Value)
                            select v;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);
                
                return query.ToList();
            }
            else
            {
                var query = from v in Context.VENTAs.Include("CLIENTE")
                            where v.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                            v.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper())
                            select v;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);
                
                return query.Take(1000).ToList();
            }
        }

        public List<VENTA> GetFromVentasbyFechaAndClienteAndFactura(DateTime? fec1, DateTime? fec2, string cliente, bool factura)
        {
            if (cliente != "")
            {
                var query = from h in Context.VENTAs.Include("CLIENTE")
                            where h.Factura == factura
                            select h;

                if (fec1 != null && fec2 != null)//ambos
                {
                    query = from h in query
                            where (h.Fecha >= fec1.Value && h.Fecha <= fec2.Value) &&
                                    (h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                                    h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper()))
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente y dia
                {
                    query = from h in query
                            where
                            (h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                            h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper())) && h.Fecha == fec1.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 == null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                            h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper())
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                else //retornar todos
                {
                    query = from h in query
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.Take(500).ToList();
            }
            else
            {
                var query = from h in Context.VENTAs.Include("CLIENTE")
                            where h.Factura == factura
                            select h;

                if (fec1 != null && fec2 != null)//solo fecha
                {
                    query = from h in query
                            where h.Fecha >= fec1.Value && h.Fecha <= fec2.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente y dia
                {
                    query = from h in query
                            where
                            h.Fecha == fec1.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 == null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.Take(500).ToList();
            }
        }

        public List<VENTA> GetFromVentasbyFechaAndClienteAndBoleta(DateTime? fec1, DateTime? fec2, string cliente)
        {
            if (cliente != "")
            {
                var query = from h in Context.VENTAs.Include("CLIENTE")
                            where h.Factura == false
                            select h;

                if (fec1 != null && fec2 != null)//ambos
                {
                    query = from h in query
                            where (h.Fecha >= fec1.Value && h.Fecha <= fec2.Value) &&
                                    (h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                                    h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper()))
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente y dia
                {
                    query = from h in query
                            where
                            (
                            h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                            h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper())
                            ) && h.Fecha == fec1.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 == null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper())
                            || h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper())
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                else //retornar todos
                {
                    query = from h in query
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.Take(500).ToList();
            }
            else
            {
                var query = from h in Context.VENTAs.Include("CLIENTE")
                            where h.Factura == false
                            select h;


                if (fec1 != null && fec2 != null)//solo fecha
                {
                    query = from h in query
                            where h.Fecha >= fec1.Value && h.Fecha <= fec2.Value
                            select h;
                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente y dia
                {
                    query = from h in query
                            where h.Fecha == fec1.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 == null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.Take(500).ToList();
            }
        }

        public List<VENTA> GetFromVentasbyFechaAndCliente(DateTime? fec1, DateTime? fec2, string cliente)
        {
            if (cliente != "")
            {
                var query = from h in Context.VENTAs.Include("CLIENTE")
                            select h;
                
                if (fec1 != null && fec2 != null)//cliente y fecha 
                {
                    query = from h in query
                            where (h.Fecha >= fec1.Value && h.Fecha <= fec2.Value) &&
                                    (h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                                    h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper()))
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where
                            (h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                            h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper())) && h.Fecha == fec1.Value
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 == null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.CLIENTE.NombreRazonSocial.ToUpper().Contains(cliente.ToUpper()) ||
                            h.CLIENTE.DniRuc.ToUpper().Contains(cliente.ToUpper())
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                else //retornar todos
                {
                    query = from h in query
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.Take(500).ToList();
            }
            else
            {
                var query = from h in Context.VENTAs.Include("CLIENTE")
                            select h;

                if (fec1 != null && fec2 != null)//solo fecha
                {
                    query = from h in query
                            where h.Fecha >= fec1.Value && h.Fecha <= fec2.Value
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.Fecha == fec1.Value
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 == null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            orderby h.Fecha descending
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.Take(500).ToList();
            }
        }

        public VENTA GetFromVentaById(int id)
        {
            var query = from dv in Context.VENTAs.Include("DETALLEVENTAs.PRODUCTO")
                        where dv.Id.Equals(id)
                        select dv;
            
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public void AddVenta(VENTA venta)
        {
            foreach (var item in venta.DETALLEVENTAs)
            {
                Context.Entry(item.PRODUCTO).State = EntityState.Unchanged;
                Context.Entry(item).State = EntityState.Added;

                //item.ProductoId = item.PRODUCTO.Id;
                //Context.Entry(item.PRODUCTO).State = EntityState.Detached;

            }
            Context.Entry(venta.CLIENTE).State = EntityState.Unchanged;
            venta.Observacion = " ";
            venta.Cancelada = false;
            Context.VENTAs.Add(venta);
            Context.SaveChanges();
        }

        public void CancelarVenta(int id)
        {
            var existe = Context.VENTAs.Find(id);
            Context.Entry(existe).State = EntityState.Modified;
            existe.Cancelada = true;
            Context.SaveChanges();
        }

        public bool GetAllVentasByImpuestoIdOrClienteId(int idImpuesto, int idCliente)
        {
            var query = from c in Context.VENTAs.Include("IMPUESTO").Include("CLIENTE")
                        where c.ImpuestoId.Equals(idImpuesto)
                        || c.ClienteId.Equals(idCliente)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }
    }
}
